package com.mac.cxcybackground.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mac.cxcybackground.bean.Activities;
import com.mac.cxcybackground.bean.Awards;
import com.mac.cxcybackground.mapper.AwardsMapper;
import com.mac.cxcybackground.service.AwardsService;
import com.mac.cxcybackground.vo.AwardsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
public class AwardsController {
    @Autowired
    AwardsMapper awardsMapper;
    @Autowired
    AwardsService awardsService;
    @GetMapping("/awards_table")
    public String AwardsFind(@RequestParam(value = "pn",defaultValue = "1")Integer pn,
                             Model model){
        List<AwardsVo> result = awardsMapper.findAllList();
        Page<Awards> activitiesPage = new Page<>(pn, 8);
        Page<Awards> page = awardsService.page(activitiesPage,null);
        model.addAttribute("page",page);
        model.addAttribute("result",result);
        return "table/awards_table";
    }
    @GetMapping("/awards_add_table")
    public String awards_add_table(){
        return "table/awards_add_table";
    }
    @PostMapping("/awards/add")
    public String AwardsAdd(Awards awards,Model model){
        Awards award =new Awards();
        award.setId(null);
        award.setSNo(awards.getSNo());
        award.setTime(awards.getTime());
        award.setActivity(awards.getActivity());
        award.setAward(awards.getAward());
        awardsService.save(award);
        model.addAttribute("success","添加成功");
        return "redirect:/awards_table";
    }
    @GetMapping("/awards/delete/{id}")
    public String AwardsDelete(@PathVariable("id") Long id,
                                   @RequestParam(value = "pn",defaultValue = "1") Integer pn,
                                   RedirectAttributes ra){
       awardsMapper.deleteById(id);
        ra.addAttribute("pn",pn);
        return "redirect:/awards_table";
    }
    @GetMapping("/awards/update-get/{id}")
    public String AwardsUpdate_get(@PathVariable("id") Long id,
                                   @RequestParam(value = "pn",defaultValue = "1") Integer pn,
                                   RedirectAttributes ra,Model model){
        Awards awards = awardsService.getById(id);
        ra.addAttribute("pn",pn);
        model.addAttribute("awards",awards);
        return "table/awards_update_table";
    }
    @GetMapping("/awards/update/{id}")
    public String AwardsUpdate(@PathVariable("id") Long id,Awards awards){
        Awards award= new Awards();
        award.setId(id);
        award.setSNo(awards.getSNo());
        award.setTime(awards.getTime());
        award.setActivity(awards.getActivity());
        award.setAward(awards.getAward());
        awardsService.updateById(award);
        return "redirect:/awards_table";

    }
}
